package com.cn.goodsadmin.service;

import com.cn.goodsadmin.po.GoodsInfo;
import com.cn.goodsadmin.po.GoodsInfoLog;
import com.cn.goodsadmin.po.Userbuylog;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service("UserBuyLogService")
public class UserBuyLogService extends BaseService {

    public List<GoodsInfoLog> findGoodsCountBytime(int day, int appid) throws Exception {
        List<GoodsInfoLog> logs = userBuyLogDao.findAllgoodsCountByTime(day, appid);
        for (GoodsInfoLog log : logs) {
            String goodsId = log.getGoodsId();
            GoodsInfo info = goodsInfoDao.findGoodsInfoByid(goodsId);
            if (info != null) {
                log.setGoodsInfoName(info.getcGoodsname());
            }
        }
        return logs;
    }

    public HashMap<String, Integer> getGoodsTimeLogByTime(int day, String goodsid, int appid) throws Exception {
        List<Userbuylog> userbuylogs = userBuyLogDao.findGoodsCardByTime(day, goodsid, appid);
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:00");
        for (Userbuylog userbuylog : userbuylogs) {
            Date ctime = userbuylog.getcCtime();
            String time = simpleDateFormat.format(ctime);
            if (map.containsKey(time)) {
                Integer value = map.get(time);
                map.put(time, value.intValue() + 1);
            } else {
                map.put(time, 1);
            }
        }
        return map;
    }

    public HashMap<String, Integer> getGoodInfoOneTimeCount(String time, int day, int appid) throws Exception {
        List<Userbuylog> userbuylogs = userBuyLogDao.findGoodsCardByTime(day, null, appid);
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:00");
        for (Userbuylog userbuylog : userbuylogs) {
            Date ctime = userbuylog.getcCtime();
            String hm = simpleDateFormat.format(ctime);
            String goodsid = userbuylog.getcGoodsid();
            GoodsInfo info = goodsInfoDao.findGoodsInfoByid(goodsid);
            if(info!=null) {
                String goodsname = info.getcGoodsname();
                if (time.equals(hm)) {
                    if (map.containsKey(goodsname)) {
                        Integer value = map.get(goodsname);
                        map.put(goodsname, value.intValue() + 1);
                    } else {
                        map.put(goodsname, 1);
                    }
                }
            }
        }
        return map;
    }
}
